<template>
  <Dialog title="编辑参数" v-model="dialogVisible" width="800px">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="160px"
      v-loading="formLoading"
    >
      <el-form-item label="XBox的地址" prop="xboxUrl">
        <el-input v-model="formData.xboxUrl" placeholder="地址"/>
      </el-form-item>
      <el-form-item label="xbox设备秘钥" prop="encryptionKey">
        <el-input v-model="formData.encryptionKey"/>
      </el-form-item>
      <el-form-item label="请求参数" prop="requestMap">
        <el-input type="textarea" v-model="formData.requestMap" autosize/>
      </el-form-item>
      <el-form-item label="参数变量映射" prop="paramKey">
        <el-input type="textarea" v-model="formData.paramMap" autosize placeholder="JSON格式对应"/>
      </el-form-item>
      <el-form-item label="固定参数" prop="fixedParam">
        <el-input type="textarea" v-model="formData.fixedParam" autosize placeholder="JSON格式对应"/>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>

<script setup lang="ts">
import {getStrDictOptions, getIntDictOptions, DICT_TYPE} from '@/utils/dict'
import * as FacilityApi from '@/api/yz/facility'

const {t} = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formData = ref({
  xboxUrl: '',
  encryptionKey: '',
  fixedParam: JSON.stringify({
    totalToilet: 8
  }),
  requestMap: JSON.stringify({
    "machineCode": '',
    "groupTag": "32",
    "tags": ["五合一_温度", "五合一_湿度", "五合一_氨气", "五合一_硫化氢","五合一_PM25"],
    "timeout": 0
  }),
  paramMap: JSON.stringify({
    temperature: '五合一_温度',
    hm: '五合一_湿度',
    pm25: '五合一_PM25',
    nh3: '五合一_氨气',
    h2s: '五合一_硫化氢'
  },null, 2)
})

const facilityId = ref(undefined)
const facilityData = ref(undefined);
const formRules = reactive({})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (id?: number) => {
  dialogVisible.value = true
  resetForm()
  // 修改时，设置数据
  if (id) {
    facilityId.value = id;
    formLoading.value = true
    try {
      const facility = await FacilityApi.getFacility(id)
      facilityData.value = facility
      if(facility.settingMapJson){
        formData.value = JSON.parse(facility.settingMapJson);
      }
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({open}) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = {
      id: facilityId.value,
      ...facilityData.value,
      settingMapJson: JSON.stringify(formData.value)
    } as unknown as FacilityApi.FacilityVO
    await FacilityApi.updateFacility(data)
    message.success(t('common.updateSuccess'))
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    machineCode:'',
    xboxUrl: '192.168.3.166',
    encryptionKey: '375c9e89fc20726e',
    fixedParam: JSON.stringify({
      totalToilet: 8
    }, null,2),
    paramMap: JSON.stringify({
      temperature: '五合一_温度',
      hm: '五合一_湿度',
      pm25: '五合一_PM25',
      nh3: '五合一_氨气',
      h2s: '五合一_硫化氢'
    },null, 2)
  }
  formRef.value?.resetFields()
}
// 确认选择坐标
const confirmPickLocation = () => {

}
</script>
